package net.vinote.smart.platform.service.action;

import java.sql.SQLException;
import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;

import net.vinote.smart.platform.comm.DateFunction;
import net.vinote.smart.platform.comm.PlatformConst;
import net.vinote.smart.platform.module.database.DBManager;
import net.vinote.smart.platform.module.database.SerialNoManager;
import net.vinote.smart.platform.module.database.bo.BizObject;
import net.vinote.smart.platform.module.database.bo.DBQuery;
import net.vinote.smart.platform.module.log.RunLogger;
import net.vinote.smart.platform.service.bean.ElectronicContract;
import net.vinote.smart.platform.service.bean.User;
import net.vinote.smart.platform.service.util.AutoInjection;

import org.json.JSONObject;


/**
 * 电子合同Action
 * 
 * @author Seer
 */
public class ElectronicContractAction {
	String[] ingoreFields = { "method", "class", "r" };
	@AutoInjection
	private HttpServletRequest request;

	@AutoInjection
	private String templateId;

	@AutoInjection
	private String objectId;

	@AutoInjection
	private String objectType;

	public String doAdd() throws SQLException {
		DBManager bm = new DBManager("contract_info");
		BizObject bo = new BizObject();
		String id = SerialNoManager.getInstance().getSerialNo("contract_info");
		bo.setAttribute("id", id);
		bo.setAttribute("templateId", templateId);
		bo.setAttribute("objectType", objectType);
		bo.setAttribute("objectId", objectId);
		User curUser = (User) (request.getSession().getAttribute("user"));
		if (curUser != null) {
			bo.setAttribute("INPUTUSERID", curUser.getUserID());
			bo.setAttribute("UPDATEUSERID", curUser.getUserID());
		}
		bo.setAttribute("INPUTDATE", DateFunction.getCurrentTime());
		bo.setAttribute("UPDATEDATE", DateFunction.getCurrentTime());
		bm.saveBizObject(bo);
		JSONObject json = new JSONObject();
		json.put("success", true);
		json.put("id", id);
		return json.toString();
	}

	public String doUpdateContent() throws SQLException {
		JSONObject json = new JSONObject();
		DBManager bm = new DBManager("contract_info");
		BizObject bo = new BizObject();
		User curUser = (User) (request.getSession().getAttribute("user"));
		if (curUser != null) {
			bo.setAttribute("UPDATEUSERID", curUser.getUserID());
		}
		bo.setAttribute("UPDATEDATE", DateFunction.getCurrentTime());
		@SuppressWarnings("unchecked")
		Enumeration<String> enumer = request.getParameterNames();
		while (enumer.hasMoreElements()) {
			String key = enumer.nextElement();
			if (ingoreParam(key)) {
				continue;
			} else if ("id".equals(key)) {
				bo.setAttribute("id", request.getParameter("id"));
			} else {
				json.put(key, request.getParameter(key));
			}
		}
		bo.setAttribute("CONTENT", json.toString());
		bm.updateBizObject(bo);
		return PlatformConst.SUCCESS;
	}

	public ElectronicContract getContractById(String id) {
		DBManager bm = new DBManager("contract_info");
		DBQuery query = bm.createQuery(new String[] { "O.id", "O.content",
				"ct.title", "ct.content", }, "O.id=? and O.templateId=ct.id",
				new String[] { id }, "contract_template ct");

		try {
			BizObject bo = query.getSingleResult();
			if (bo != null) {
				ElectronicContract contract = new ElectronicContract();
				contract.setId(id);
				contract.setTemplate(bo.getAttribute("CT.CONTENT"));
				contract.setTitle(bo.getAttribute("ct.title"));
				contract.setValue(bo.getAttribute("content"));
				return contract;
			}

		} catch (SQLException e) {
			RunLogger.getLogger().log(e);
		}
		return null;
	}

	private boolean ingoreParam(String name) {
		for (String key : ingoreFields) {
			if (key.equals(name)) {
				return true;
			}
		}
		return false;
	}
}
